package xyz.yuzegod.megawalls.classes.normal.shaman;

import net.minecraft.server.v1_8_R3.Entity;
import net.minecraft.server.v1_8_R3.World;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.plugin.Plugin;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.classes.Classes;
import xyz.yuzegod.megawalls.classes.Skill;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.nms.ShamanWolf;
import xyz.yuzegod.megawalls.stats.KitStatsContainer;
import xyz.yuzegod.megawalls.util.EntityTypes;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ThirdSkill extends Skill
{
    private Map<GamePlayer, List<ShamanWolf>> wolves;
    
    public ThirdSkill(final Classes classes) {
        super("狼群", classes);
        this.wolves = new HashMap<GamePlayer, List<ShamanWolf>>();
    }
    
    @Override
    public int maxedLevel() {
        return 3;
    }
    
    @Override
    public double getAttribute(final int level) {
        switch (level) {
            case 1: {
                return 4.0;
            }
            case 2: {
                return 8.0;
            }
            case 3: {
                return 12.0;
            }
            default: {
                return 4.0;
            }
        }
    }
    
    @Override
    public List<String> getInfo(final int level) {
        final List<String> lore = new ArrayList<String>();
        lore.add(" §8▪ §7被攻击时,有10%的几率召唤");
        lore.add("   §7一只保护你§a" + this.getAttribute(level) + "§7秒的狼。");
        return lore;
    }
    
    @Override
    public void upgrade(final GamePlayer gamePlayer) {
        final KitStatsContainer kitStats = gamePlayer.getPlayerStats().getKitStats(this.getClasses());
        kitStats.addSkill3Level();
    }
    
    @Override
    public int getPlayerLevel(final GamePlayer gamePlayer) {
        return gamePlayer.getPlayerStats().getKitStats(this.getClasses()).getSkill3Level();
    }
    
    @Override
    public boolean use(final GamePlayer gamePlayer, final KitStatsContainer kitStats) {
        if (MegaWalls.getRandom().nextInt(100) <= 10) {
            final List<ShamanWolf> list = this.wolves.getOrDefault(gamePlayer, new ArrayList<ShamanWolf>());
            if (list.size() >= 3) {
                return false;
            }
            final ShamanWolf wolf = new ShamanWolf((World)((CraftWorld)Bukkit.getWorld("world")).getHandle());
            wolf.setGamePlayer(gamePlayer);
            wolf.getBukkitEntity().setMetadata(MegaWalls.getMetadataValue(), (MetadataValue)new FixedMetadataValue((Plugin)MegaWalls.getInstance(), (Object)gamePlayer.getGameTeam()));
            EntityTypes.spawnEntity((Entity)wolf, gamePlayer.getPlayer().getLocation());
            list.add(wolf);
            this.wolves.put(gamePlayer, list);
            Bukkit.getScheduler().runTaskLater((Plugin)MegaWalls.getInstance(), (Runnable)new Runnable() {
                @Override
                public void run() {
                    if (!wolf.dead) {
                        wolf.die();
                    }
                    final List<ShamanWolf> list = ThirdSkill.this.wolves.getOrDefault(gamePlayer, new ArrayList<ShamanWolf>());
                    list.remove(wolf);
                    ThirdSkill.this.wolves.put(gamePlayer, list);
                }
            }, (long)this.getAttribute(this.getPlayerLevel(gamePlayer)) * 20L);
        }
        return true;
    }
}
